﻿using CommonServiceLocator;
using DataManagementBoard.Model;
using DataManagementBoard.Views;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Ioc;
using GalaSoft.MvvmLight.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace DataManagementBoard.ViewModel
{
    public class LoginWindowViewModel :ViewModelBase
    {
        private AppData appData;

        public LoginWindowViewModel()
        {
            appData = ServiceLocator.Current.GetInstance<AppData>();
        }

        public AppData AppData
        {
            get { return appData; }
            set { appData = value; }
        }

        public ICommand CheckUserCommand
        {
            get
            {
                //回调函数
                var command = new RelayCommand(() => {
                    BoardDBEntities db = new BoardDBEntities();
                    var model = db.UserInfo.ToList().Where(t => t.Name == AppData.CurrentUser.Name && t.Password == AppData.CurrentUser.Password).FirstOrDefault();
                    if (model == null)
                    {
                        return;
                    }
                    AppData.CurrentUser = model;

                });
                return command;
            }
        }


        public RelayCommand<LoginWindow> CheckUserCommand2
        {
            get
            {
                //回调函数
                var command = new RelayCommand<LoginWindow>((loginWindow) => {
                    BoardDBEntities db = new BoardDBEntities();
                    var model = db.UserInfo.ToList().Where(t => t.Name == AppData.CurrentUser.Name && t.Password == AppData.CurrentUser.Password).FirstOrDefault();
                    if (model == null)
                    {
                        var dia = SimpleIoc.Default.GetInstance<IDialogService>();
                        dia.ShowMessageBox("用户名或者密码错误", "提示");
                        return;
                    }

                    CacheHelper.CacheInsert(model.Name,model);

                    AppData.CurrentUser = model;
                    MainWindow mainWindow = new MainWindow();
                    mainWindow.Show();
                    loginWindow.Close();
                });
                return command;
            }
        }




    }
}
